if SERVER then
	AddCSLuaFile("shared.lua")
end

if CLIENT then
	SWEP.PrintName = "Arrest Baton"
	SWEP.Slot = 1
	SWEP.SlotPos = 3
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
end

SWEP.Base = "weapon_cs_base2"

SWEP.Author = "Rick Darkaliono, philxyz"
SWEP.Instructions = "Left or right click to arrest"
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.IconLetter = ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix = "stunstick"

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.NextStrike = 0

SWEP.ViewModel = Model("models/weapons/v_stunbaton.mdl")
SWEP.WorldModel = Model("models/weapons/w_stunbaton.mdl")

SWEP.Sound = Sound("weapons/stunstick/stunstick_swing1.wav")

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false 
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

function SWEP:Initialize()
	self:SetWeaponHoldType("normal")
end

function SWEP:Holster()
	if CLIENT then return end
	return true
end

function SWEP:PrimaryAttack()
	if CurTime() < self.NextStrike then return end
	
	self:SetWeaponHoldType("melee")
	timer.Simple(0.3, function(wep) if wep:IsValid() then wep:SetWeaponHoldType("normal") end end, self)
	
	self.Owner:SetAnimation(PLAYER_ATTACK1)
	self.Weapon:EmitSound(self.Sound)
	self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)

	self.NextStrike = CurTime() + .4
	
	if CLIENT then return end
	
	local trace = self.Owner:GetEyeTrace()
	
	if ValidEntity(trace.Entity) and trace.Entity:IsPlayer() and (trace.Entity:Team() == 2) then
		evolve:Notify(self.Owner, evolve.colors.red, "You can not arrest other CPs!")
		return
	end
		
	if not ValidEntity(trace.Entity) or (self.Owner:EyePos():Distance(trace.Entity:GetPos()) > 115) or (not trace.Entity:IsPlayer() and not trace.Entity:IsNPC()) then
		return
	end
	
	if not tobool(GetConVarNumber("npcarrest")) and trace.Entity:IsNPC() then
		return
	end
	
	if ( trace.Entity.EV_Jailed ) then
		evolve:Notify(trace.Entity, evolve.colors.white, "You have been moved back to your cell!")
		evolve:Notify(self.Owner, evolve.colors.blue, trace.Entity:Nick(), evolve.colors.white, " was moved back to their cell!")
		trace.Entity:SetPos(evolve.jailPos)
		return
	end
	
	if not ( trace.Entity.Wanted ) then
		evolve:Notify(self.Owner, evolve.colors.blue, trace.Entity:Nick(), evolve.colors.white, " is not wanted by police!")
		return
	end

	/*
	if GetConVarNumber("needwantedforarrest") == 1 and not trace.Entity:IsNPC() and not trace.Entity.DarkRPVars.wanted then
		Notify(self.Owner, 1, 5, "The player must be wanted in order to be able to arrest them.")
		return
	end
	*/
	
	local jpc = evolve.jailPosCount
	local jailtime = GetConVarNumber("rp_jailtime")
	
	if not jpc or jpc == 0 then
		evolve:Notify(self.Owner, evolve.colors.red, "You cannot arrest people since there are no jail positions set!")
	else
		-- Send NPCs to Jail
		if trace.Entity:IsNPC() then
			trace.Entity:SetPos(evolve.jailPos)
		else
			self.Owner:ConCommand("ev jail \"".. trace.Entity:SteamID() .. "\"")
			evolve:Notify(trace.Entity, evolve.colors.white, "You've been arrested by ", evolve.colors.blue, self.Owner:Nick())
			evolve:Notify(self.Owner, evolve.colors.white, "You've arrested: ", evolve.colors.blue, trace.Entity:Nick())
			evolve:Notify(trace.Entity, evolve.colors.white, "You will be released in: ", evolve.colors.blue, tostring(jailtime), evolve.colors.white, " seconds.")
			timer.Create("JailTimer", tonumber(jailtime), 1, function()
			self.Owner:ConCommand("evs jail \"".. trace.Entity:SteamID() .. "\" 0")
			evolve:Notify(trace.Entity, evolve.colors.white, "You have been released!")
			end)
		end
	end
end

function SWEP:SecondaryAttack()
	self:PrimaryAttack()
end
